import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, ParseUUIDPipe } from '@nestjs/common';
import { ListService } from './list.service';
import { CreateListDto } from './dto/create-list.dto';
import { UpdateListDto } from './dto/update-list.dto';
import * as uuid from 'uuid';
console.log('生成的uuid:', uuid.v4());

@Controller('list')
export class ListController {
  constructor(private readonly listService: ListService) {}

  @Post()
  create(@Body() createListDto: CreateListDto) {
    return this.listService.create(createListDto);
  }

  @Get()
  findAll() {
    return this.listService.findAll();
  }

  @Get('one')
  one() {
    return {
      code: 200,
      msg: 'this is one get list'
    };
  }


// pipe管道校验转化
// nestjs内置的管道API
// ValidationPipe
// ParseIntPipe
// ParseFloatPipe
// ParseBoolPipe
// ParseArrayPipe
// ParseUUIDPipe
// ParseEnumPipe
// DefaultValuePipe

// 案例1 我们接受的动态参数希望是一个number 类型 现在是string 
  // @Get(':id')
  // findOne(@Param('id', ParseIntPipe) id: number) {
  //   console.log('list--get---->', typeof id, id);
  //   return this.listService.findOne(+id);
  // }

  // 案例2 验证UUID
  // npm install uuid -S
  // npm install @types/uuid -D

  // 生成一个uuid
  @Get(':id')
  findOneUUid(@Param('id', ParseUUIDPipe) id: string) {
    console.log('list--findOneUUid---->', id);
    return this.listService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateListDto: UpdateListDto) {
    return this.listService.update(+id, updateListDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.listService.remove(+id);
  }
}
